package com.linzh.commonbase.controller;

import com.linzh.commonbase.pojo.R;
import com.linzh.commonbase.pojo.User;
import com.linzh.commonbase.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    private UserService service;
    @RequestMapping("hello")
    public Object hello(String user_name){
        return String.format("Hello %s",user_name!=null?user_name:"");
    }
    @RequestMapping("register")
    public Object register(String user_name,String password,String tel,String email){
        //根据user_name 查询用户是否存在
        User u = service.findByUserName(user_name);
        if(u==null){
            //不存在则新增用户
            u = new User();
            u.setUser_name(user_name);
            u.setPassword(password);
            u.setTel(tel);
            u.setEmail(email);
            service.addUser(u);
            return R.success();
        }else{
            return R.error("用户名已存在");
        }
    }
    @RequestMapping("login")
    public Object login(String user_name,String password){
        User u = service.findByUserName(user_name);
        if(u==null){
            return R.error("用户不存在");
        }else{
            if(u.getPassword().equals(password)){
                return R.success();
            }else{
                return R.error("密码不正确");
            }
        }
    }
    @RequestMapping("freezeUser")
    public Object freezeUser(int id){
        int i = service.freezeUser(id);
        if(i==0){
            return R.error(null);
        }else{
            return R.success();
        }
    }
    @RequestMapping("findUserList")
    public Object findUserList(){
        return R.success(service.findUserList());
    }
}
